package com.madou.dbtool.redis.test;

import com.formdev.flatlaf.extras.components.FlatButton;
import com.madou.common.safe.utils.DecodeClassLoader;
import com.madou.common.safe.utils.DecodeClassLoaderUtil;
import com.madou.dbtool.common.enums.DBEnum;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.event.DBEventManager;
import com.madou.dbtool.redis.constants.RedisIds;
import com.madou.dbtool.redis.dialog.ConnectionDialog;
import com.madou.dbtool.redis.event.OpenRedisClientEvent;
import com.madou.dbtool.redis.event.OpenRedisDataEvent;
import com.madou.dbtool.redis.node.RedisTreeNodeInfo;
import com.madou.dbtool.relationship.frame.BaseDBToolFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Redis数据库加载类
 * @author miukoo
 */
public class RedisDBToolFrame  extends BaseDBToolFrame {

    @Override
    public void initEditorLanguange() {
        DecodeClassLoader decodeClassLoader = DecodeClassLoaderUtil.getDecodeClassLoader();
        try {
            Class<?> instance = decodeClassLoader.findClassByAlias("redisContentTypeParser","com.madou.dbtool.language.redis.parser.RedisContentTypeParser");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void initButton(){
        java.util.List<JComponent> component = new ArrayList<>();
        this.initConnectionButton(component);
        this.initClientButton(component);
        this.initDataButton(component);
        initMFrameBar(component);
    }

    @Override
    public DBEnum getDBEnum() {
        return DBEnum.REDIS;
    }

    private void initConnectionButton(List<JComponent> component) {
        FlatButton connectionButton = createButton( DBToolBundle.message("mysql.top.button.connect"), AllIcons.Common.FORMAT);
        connectionButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ConnectionDialog.actionRedisConnect(UUID.randomUUID().toString(), DBToolBundle.message("connect.button.add"),null);
            }
        });
        component.add(connectionButton);
    }

    private void initDataButton(List<JComponent> component) {
        FlatButton tableButton = createButton(DBToolBundle.message("mysql.top.button.data"),AllIcons.MENU.TABLE_DATA);
        tableButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JTree tree = dataSourcePanel.getTree();
                Object component = tree.getLastSelectedPathComponent();
                if(component!=null&& component instanceof RedisTreeNodeInfo){
                    RedisTreeNodeInfo info = (RedisTreeNodeInfo)component;
                    if(info.getRedisSourceManager()!=null&&info.getRedisNodeInfo()!=null) {
                        DBEventManager.notice(new OpenRedisDataEvent(RedisIds.EVENT_ID_CLIENT+"0",info.getRedisSourceManager(),info.getRedisNodeInfo(),""));
                    }
                }
            }
        });
        component.add(tableButton);
    }

    private void initClientButton(List<JComponent> component) {
        FlatButton tableButton = createButton(DBToolBundle.message("redis.top.button.client"),AllIcons.Common.TERMINAL);
        tableButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JTree tree = dataSourcePanel.getTree();
                Object component = tree.getLastSelectedPathComponent();
                if(component!=null&& component instanceof RedisTreeNodeInfo){
                    RedisTreeNodeInfo info = (RedisTreeNodeInfo)component;
                    if(info.getRedisSourceManager()!=null&&info.getRedisNodeInfo()!=null) {
                        DBEventManager.notice(new OpenRedisClientEvent(RedisIds.EVENT_ID_CLIENT+"0",info.getRedisSourceManager(),info.getRedisNodeInfo(),info.getIndex()));
                    }
                }
            }
        });
        component.add(tableButton);
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("awt.useSystemAAFontSettings", "on");
        System.setProperty("swing.aatext", "true");
//        UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
//        String name = IdeaSkinLookAndFeel.class.getName();
//        UIManager.setLookAndFeel(name);
        RedisDBToolFrame dbToolFrame = new RedisDBToolFrame();
        dbToolFrame.installThemeAndInit();
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(dbToolFrame.getGraphicsConfiguration());
        Rectangle bounds = dbToolFrame.getGraphicsConfiguration().getBounds();
        int height = bounds.height-screenInsets.bottom;
        dbToolFrame.setBounds(new Rectangle(screenInsets.left,screenInsets.top,bounds.width,height));
        dbToolFrame.setSize(new Dimension(bounds.width,height));
        dbToolFrame.setPreferredSize(new Dimension(bounds.width,height));
        dbToolFrame.setMaximumSize(new Dimension(bounds.width,height));
        dbToolFrame.setTitle(DBToolBundle.message("redis.title"));
        dbToolFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dbToolFrame.setIconImage(AllIcons.Windows.REDIS_ICON);
        dbToolFrame.setVisible(true);
        dbToolFrame.pack();
        dbToolFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
}
